<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/17
 * Time: 9:41
 */

namespace app\admin\controller;


use think\Db;

class Member extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $user_create_id = trim(input('user_create_id'), ' ');
            $user_phone = trim(input('user_phone'), ' ');
            $user_real_name = trim(input('user_real_name'), ' ');
            $user_status = input('user_status');
            $user_gender = input('user_gender');
            $user_real_check = input('user_real_check');
            $user_is_tuijian_like = input('user_is_tuijian_like');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $where['user_status'] = ['IN', [1,3,4]];
            if(isset($user_create_id) && $user_create_id != ''){
                $where['user_create_id'] = ['like', '%' . $user_create_id . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($user_real_name) && $user_real_name != ''){
                $where['user_real_name'] = ['like', '%' . $user_real_name . '%'];
            }
            if(isset($user_status) && $user_status != ''){
                $where['user_status'] = $user_status;
            }
            if(isset($user_gender) && $user_gender != ''){
                $where['user_gender'] = $user_gender;
            }
            if(isset($user_real_check) && $user_real_check != ''){
                $where['user_real_check'] = $user_real_check;
            }
            if(isset($user_is_tuijian_like) && $user_is_tuijian_like != ''){
                $where['user_is_tuijian_like'] = $user_is_tuijian_like;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['user_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['user_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['user_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')
                ->join('up_club', 'club_id = user_club_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $count2 = Db::name('u_user')
                ->join('up_club', 'club_id = user_club_id', 'left')
                ->where($where)
                ->column('user_id');// 获取总条数
            $lists = Db::name('u_user')
                ->join('up_club', 'club_id = user_club_id', 'left')
                ->field('user_id, user_create_id, user_username, user_real_name, user_gender, 
                user_phone, user_email, user_time, club_name, user_urg_phone, user_status, 
                user_is_tuijian_like')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('user_time desc')
                ->select();
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 用户详情
     * @return mixed
     */
    public function userDetail(){
        $user_id = input('user_id');
        $user = Db::table('up_u_user')
            ->join('up_area ua1', 'ua1.district_id = user_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = user_shi', 'left')
            ->field('user_id, user_username, user_image, user_intro, user_gender, ua1.district sheng, 
            ua2.district shi, user_birthday, user_ID_number, user_email, user_urg_phone, user_sailing_time, 
            user_real_check, user_watch, user_is_tuijian_like, user_qq_nickname, user_wx_nickname, 
            user_apple_token, user_fanlian_ID, user_fanxie_ID')
            ->where('user_id', $user_id)
            ->find();
        // 粉丝数
        $fans_num = Db::table('up_like')->where('like_by_user_id', $user_id)->count();
        // 关注用户数
        $like_num = Db::table('up_like')->where('like_user_id', $user_id)->count();
        // 发布动态数
        $info_num = Db::table('up_information')
            ->where('info_user_id', $user_id)
            ->where('info_is_publish', 2)
            ->where('info_status', 1)
            ->count();
        // 动态草稿数
        $info_caogao_num = Db::table('up_information')
            ->where('info_user_id', $user_id)
            ->where('info_is_publish', 1)
            ->count();
        // 赛事收藏数量
        $like_match_num = Db::table('up_my_match')->where('mm_user_id', $user_id)->count();
        $this->assign([
            'user' => $user,
            'fans_num' => $fans_num,
            'like_num' => $like_num,
            'info_num' => $info_num,
            'info_caogao_num' => $info_caogao_num,
            'like_match_num' => $like_match_num
        ]);
        return $this->fetch('member/userdetail');
    }
    
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function userFreeze(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 3);
        if($res){
           // 发送短信到被冻结用户
           $user_phone = Db::table('up_u_user')->where('user_id', $user_id)->value('user_phone');
           $server_phone = Db::table('up_other')->where('other_id', 4)->value('other_phone');
           YzxSms($server_phone, $user_phone, '', 4);
           return json(['code' => 200, 'msg' => '冻结成功']);
        }else{
           return json(['code' => 100, 'msg' => '冻结失败']);
        }
    }
    
    /**
     * 解冻
     * @return \think\response\Json
     */
    public function userStartUsing(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '解冻成功']);
        }else{
            return json(['code' => 100, 'msg' => '解冻失败']);
        }
    }
    
    /**
     * 恢复注销
     * @return \think\response\Json
     */
    public function userStartRecover(){
        $user_id = input('id');
        $user = Db::table('up_u_user')
            ->where('user_id', $user_id)
            ->field('user_phone')
            ->find();
        Db::startTrans();
        try{
            Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 1);
            Db::table('up_logout_apply')->where('apply_user_phone', $user['user_phone'])->delete();
            Db::commit();
            return json(['code' => 200, 'msg' => '恢复成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '恢复失败']);
        }
    }
    
    /**
     * 推荐
     * @return \think\response\Json
     */
    public function userCommend(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_is_tuijian_like', 2);
        if($res){
            return json(['code' => 200, 'msg' => '推荐成功']);
        }else{
            return json(['code' => 100, 'msg' => '推荐失败']);
        }
    }
    
    /**
     * 取消推荐
     * @return \think\response\Json
     */
    public function userUncommend(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_is_tuijian_like', 1);
        if($res){
            return json(['code' => 200, 'msg' => '取消推荐成功']);
        }else{
            return json(['code' => 100, 'msg' => '取消推荐失败']);
        }
    }
    
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $user_ids = input('ids');
        if($user_ids == ''){
            return json(['code' => 100, 'msg' => '请选择要操作的用户']);
        }
        $lists = Db::name('u_user')
            ->join('up_club', 'club_id = user_club_id', 'left')
            ->join('up_area ua1', 'ua1.district_id = user_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = user_shi', 'left')
            ->field('user_create_id, user_username, user_phone, user_gender, user_is_tuijian_like, 
            user_birthday, user_real_check, user_real_name, user_ID_number, user_email, user_call, 
            user_urg_phone, club_name, user_status, user_watch, user_intro, ua1.district sheng, 
            ua2.district shi, user_apple_token, user_qq_nickname, user_wx_nickname, user_sailing_time, 
            user_time, user_last_time')
            ->where('user_id', 'IN', $user_ids)
            ->order('user_time desc')
            ->select();
        foreach($lists as $k => $v){
            // 性别
            if($v['user_gender'] == 1){
                $lists[$k]['gender'] = '男';
            }elseif($v['user_gender'] == 2){
                $lists[$k]['gender'] = '女';
            }else{
                $lists[$k]['gender'] = '未知';
            }
            // 推荐风讯
            $lists[$k]['tuijian'] = $v['user_is_tuijian_like'] == 1 ? '否' : '是';
            // 实名认证
            $lists[$k]['real_check'] = $v['user_real_check'] == 1 ? '否' : '是';
            // 用户状态
            $lists[$k]['user_status'] = $v['user_status'] == 1 ? '正常' : '冻结';
            // 手机号
            $lists[$k]['user_phone'] = "\t".$v['user_phone']."\t";
            // 电话
            $lists[$k]['user_call'] = "\t".$v['user_call']."\t";
            // 紧急联系人电话
            $lists[$k]['user_urg_phone'] = "\t".$v['user_urg_phone']."\t";
            // 用户ID
            $lists[$k]['user_create_id'] = "\t".$v['user_create_id']."\t";
            // 身份证号
            $lists[$k]['user_ID_number'] = "\t".$v['user_ID_number']."\t";
            // 城市
            $lists[$k]['district'] = $v['sheng'] . $v['shi'];
            // 处理特殊字符串
            $user_name = json_encode($v['user_username']);
            $name = preg_replace("/\\\u[ed][0-9a-f]{3}\\\u[ed][0-9a-f]{3}/", "*", $user_name);//将特殊字符串替换成*
            $lists[$k]['user_username'] = json_decode($name);
    
            $user_qq = json_encode($v['user_qq_nickname']);
            $name_qq = preg_replace("/\\\u[ed][0-9a-f]{3}\\\u[ed][0-9a-f]{3}/", "*", $user_qq);//将特殊字符串替换成*
            $lists[$k]['user_qq_nickname'] = json_decode($name_qq);
    
            $user_wx = json_encode($v['user_wx_nickname']);
            $name_wx = preg_replace("/\\\u[ed][0-9a-f]{3}\\\u[ed][0-9a-f]{3}/", "*", $user_wx);//将特殊字符串替换成*
            $lists[$k]['user_wx_nickname'] = json_decode($name_wx);
        }
        $cellname = [
            ['user_create_id', '用户ID', 15, 'LEFT'],
            ['user_username', '昵称', 15, 'LEFT'],
            ['user_phone', '手机号', 20, 'LEFT'],
            ['gender', '性别', 10, 'LEFT'],
            ['tuijian', '推荐（风讯）', 15, 'LEFT'],
            ['user_birthday', '出生日期', 15, 'LEFT'],
            ['real_check', '实名认证', 15, 'LEFT'],
            ['user_real_name', '真实姓名', 15,'LEFT'],
            ['user_ID_number', '身份证号', 20, 'LEFT'],
            ['user_email', '邮箱', 30, 'LEFT'],
            ['user_call', '电话', 20, 'LEFT'],
            ['user_urg_phone', '紧急联系人手机号', 20, 'LEFT'],
            ['club_name', '俱乐部', 20, 'LEFT'],
            ['user_status', '用户状态', 15, 'LEFT'],
            ['user_watch', 'iBoat Watch', 20, 'LEFT'],
            ['user_intro', '简介', 30, 'LEFT'],
            ['district', '城市', 25, 'LEFT'],
            ['user_apple_token', '绑定apple', 15, 'LEFT'],
            ['user_qq_nickname', '绑定QQ', 15, 'LEFT'],
            ['user_wx_nickname', '绑定微信', 15, 'LEFT'],
            ['user_sailing_time', '航海起航时间', 20, 'LEFT'],
            ['user_time', '注册时间', 20, 'LEFT'],
            ['user_last_time', '最后登录时间', 20, 'LEFT']
        ];
        $res = exportExcel('用户列表','Members',$cellname,$lists);
        return json($res);
    }
    
    /**
     * 注销用户
     * @return mixed|\think\response\Json
     */
    public function logoutUser(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('logout_apply')->count();// 获取总条数
            $lists = Db::name('logout_apply')
                ->field('apply_user_name, apply_user_phone, apply_user_gender, apply_user_email, 
                apply_user_urg_phone, apply_reason, apply_time')
                ->page($Nowpage,$limits)
                ->order('apply_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('member/logoutuser');
    }
}